import Food from './Food';
import ScorePanel from "./ScorePanel";
import Snake from "./Snake";
export default class GameControl {
    constructor() {
        this.direction = "";
        this.isLive = true;
        this.isPuash = false;
        this.food = new Food();
        this.scorePanel = new ScorePanel();
        this.snake = new Snake();
        this.init();
    }
    init() {
        document.addEventListener("keydown", this.handleKeyDown.bind(this));
        this.food.change();
        this.run();
    }
    handleKeyDown(e) {
        this.direction = e.key;
    }
    run() {
        let X = this.snake.X;
        let Y = this.snake.Y;
        switch (this.direction) {
            case "ArrowUp":
                Y -= 10;
                break;
            case "ArrowDown":
                Y += 10;
                break;
            case "ArrowLeft":
                X -= 10;
                break;
            case "ArrowRight":
                X += 10;
                break;
            case " ":
                console.log(1);
                break;
        }
        if (this.food.X === X && this.food.Y === Y) {
            this.snake.addBody();
            this.food.change();
            this.scorePanel.addScore();
        }
        try {
            this.snake.X = X;
            this.snake.Y = Y;
        }
        catch (error) {
            this.isLive = false;
            alert(error.message + " GAME OVER!");
        }
        if (this.isLive && !this.isPuash) {
            setTimeout(() => {
                this.run();
            }, 180 - (+this.scorePanel.level.innerHTML - 1) * 20);
        }
    }
}
